<!--

/*
* Copyright (C) 2005-2013 University of Sydney
*
* Licensed under the GNU License, Version 3.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/**
* brief description of file
*
* @author      Tom Murtagh
* @author      Kim Jackson
* @author      Ian Johnson   <ian.johnson@sydney.edu.au>
* @author      Stephen White   <stephen.white@sydney.edu.au>
* @author      Artem Osmakov   <artem.osmakov@sydney.edu.au>
* @copyright   (C) 2005-2013 University of Sydney
* @link        http://Sydney.edu.au/Heurist
* @version     3.1.0
* @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
* @package     Heurist academic knowledge management system
* @subpackage  !!!subpackagename for file such as Administration, Search, Edit, Application, Library
*/

-->
<!--

/**
 * filename, brief description, date of creation, by whom
 * @copyright (C) 2005-2010 University of Sydney Digital Innovation Unit.
 * @link: http://HeuristScholar.org
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @package Heurist academic knowledge management system
 * @todo
 **/

-->
<html>
	<head>
		<link rel=stylesheet href="../../../common/css/global.css">
		<link rel=stylesheet href="../../../common/css/edit.css">
		<link rel=stylesheet href="../../../common/css/autocomplete.css">
		<!--link rel=stylesheet href="../../../common/css/woot.css"-->
		<link rel="stylesheet" type="text/css" href="../../../common/css/calendar.css" />
		<script type="text/javascript" src="../../../external/jquery/jquery.js"></script>
		<script type="text/javascript" src="../../../common/js/hintDiv.js"></script>
	</head>
	<body class="editTab">

		<script>
			top.HAPI.importSymbols(top, this);
		</script>

		<script src="../../../external/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
		<script src="../../woot/woot.js"></script>

		<script src="../../../common/js/utilsLoad.js"></script>
		<script src="../../../common/php/displayPreferences.php"></script>
		<script src="../../tags/autocompleteTags.js"></script>

		<script type="text/javascript" src="../../../common/js/calendarViewer.js"></script>
		<script type="text/javascript" src="../../../common/js/calendar.js"></script>

		<script>
			var db = (top.HAPI.database || top.HEURIST.database.name || top.HEURIST.parameters.db);
			document.write('<form method=post jsonAction="'+top.HEURIST.basePath+'records/bookmarks/saveBookmarkData.php?db='+db+'" action="'+top.HEURIST.basePath+'records/bookmarks/saveBookmarkData.php?db='+db+'" onsubmit="return verifyForm();">');
		</script>

		<script>
			function onshow() {
				top.HEURIST.edit.calendarViewer = calendarViewer;
				setTimeout(function() { try { document.getElementById("tags").focus(); } catch (e) {} }, 0);
			}
		</script>

		<input type=hidden name=save-mode id=save-mode value=edit>
		<input type=hidden name=bkmk_id id=bkmk_id>

		<div id=all-inputs>
				<div class=input-row>
					<div class=input-header-cell>Personal tags
						<div class="help prompt">
							Type as many tags as you like, separated by commas.  Tags may contain spaces.
							New tags are added automatically and are specific to each user.<br><br>
							<a href="../../../help/tags.html" onClick="top.HEURIST.util.popupURL(window, this.href); return false;">How to enter tags</a>
						</div>
					</div>
					<div class=input-cell>
						<input id=tags name=tagString type=text>
						<div id=top-tags-cell><span class=prompt>Top:</span>
						<a href="#" class=add-tag-link onClick="addTag('Favourites'); return false;">Favourites </a><a href="#" class=add-tag-link onClick="addTag('To Read'); return false;"> To Read </a></div>
						<div id=recent-tags-cell>
							<span class=prompt>Recent:</span>
						</div>
					</div>
				</div>

				<div class=input-row>
					<div class=input-header-cell>Rating<div class="help prompt">Please enter your rating for this Record.</div></div>
					<div class=input-cell>
						<div class=rating-row><span class=header>Interest</span><span><select id=overall-rating name=overall-rating></select></span></div>
					</div>
				</div>

				<div class=input-row>
					<div class=input-header-cell>Private notes<div class="help prompt">These notes are private and visible only to you.</div> </div>
					<div class=input-cell>
						<fieldset>
						<legend>Personal Notes</legend>
							<div id=personalWoot></div>
							<div><a id=woot-ext-link target=_blank href="../../woot/woot.html">open in new window</a></div>
						</fieldset>
					</div>
				</div>

				<div class=input-row>
					<div class=input-header-cell>Password reminder<div class="help prompt">NOT ENCRYPTED - do not use for secure passwords</div></div>
					<div class=input-cell>
						<input type=text class=in name=password-reminder id=password-reminder style="width: 200px;" onChange="window.changed();" onKeyPress="window.changed();">

					</div>
				</div>
		</div>
		</form>

		<form method=post jsonAction="records/reminders/saveReminder.php" action="records/reminders/saveReminder.php" onSubmit="return false;">
			<div><!-- separate table, because this is a separate form -->
					<div class=input-row>
						<div class=input-header-cell>Reminders</div>
						<div class=input-cell id=reminders></div>
			</div></div>
		</form>

		<script>


			var db = (top.HAPI.database || top.HEURIST.database.name || top.HEURIST.parameters.db);
			var tags = document.getElementById("tags");
			tags.value = tags.defaultValue = top.HEURIST.edit.record.tagString  ||  "";

			new top.HEURIST.autocomplete.AutoComplete(tags, top.HEURIST.util.tagAutofill, { nonVocabularyCallback: top.HEURIST.util.showConfirmNewTag });


			tags.onchange = function() { top.HEURIST.edit.changed("personal"); };

			function addTag(tag) {
				var tags = document.getElementById("tags");

				var tagPos = tags.value.indexOf(tag);
				if (tagPos != -1) {
					if (tags.value.substring(0, tagPos).match(/(?:^|,)\s*$/)  &&
					tags.value.substring(tagPos + tag.length).match(/^\s*(?:,|$)/)) {
						// tag is already there
						return;
					}
				}

				if (tags.value) tags.value += "," + tag;
				else tags.value = tag;

				tags.onchange();

				tags.focus();
			}
			function addTagLink(tag, parentNode) {
				var newLink = document.createElement("a");
				newLink.href = "#";
				newLink.className = "add-tag-link";
				newLink.onclick = function() { addTag(tag); return false; };
				newLink.appendChild(document.createTextNode(tag));
				var nobr = document.createElement("nobr");
				nobr.appendChild(newLink);
				parentNode.appendChild(nobr);
			}

			var alphasort = function(a,b) { a = a.toLowerCase(); b = b.toLowerCase(); return (a < b)? -1 : (a > b)? 1 : 0; };

			var topTagsCell = document.getElementById("top-tags-cell");
			var topTags = top.HEURIST.user.topTags.slice(0);
			topTags.sort(alphasort);
			for (var i=0; i < topTags.length; ++i) {
				addTagLink(topTags[i], topTagsCell);
			}
			var recentTagsCell = document.getElementById("recent-tags-cell");
			var recentTags = top.HEURIST.user.recentTags.slice(0, 8);
			recentTags.sort(alphasort);
			for (var i=0; i < recentTags.length; ++i) {
				addTagLink(recentTags[i], recentTagsCell);
			}

		</script>
		<script>

			function verifyForm() {
				if (tags.autocompleteObject.confirmImg) {	// user has a brand new tag waiting to be completed
					setTimeout(function() { tags.autocompleteObject.currentWordOkay(); }, 0);
					return false;
				}
				else return true;
			}

			function changed() { top.HEURIST.edit.changed("personal"); }
			function unchanged() { top.HEURIST.edit.unchanged("personal"); }
			function handleSaved(json) {
				var vals = eval(json.responseText);
				for (var i in vals)
				parent.HEURIST.edit.record[i] = vals[i];
				window.location.reload();
			}
			document.forms[0].heuristSubmit = function() {
				top.HEURIST.util.xhrFormSubmit(document.forms[0], handleSaved)
			};
			document.forms[0].heuristForceSubmit = function() {
				if (wootEditor.unlockedChunk) {
					wootEditor.save();
				}
				window.reminderInput.save(window.reminderInput.nowRadioButton.checked, true);
			};

			function fillRatings(element, values, defaultValue) {
				var i = 0;
				top.HEURIST.registerEvent(element, "change", changed);
				for (var index in values) {
					element.options[i] = new Option(values[index], index);
					if (index == defaultValue) element.selectedIndex = i;
					++i;
				}
			}
			fillRatings(document.getElementById("overall-rating"), top.HEURIST.ratings, parent.HEURIST.edit.record.rating);

			if (parent.HEURIST.edit.record.passwordReminder) {
				document.getElementById("password-reminder").value = parent.HEURIST.edit.record.passwordReminder;
			}

			document.getElementById("bkmk_id").value = top.HEURIST.edit.record.bkmkID  ||  0;

			var reminderDiv = document.getElementById("reminders");
			for (var i=0; i < parent.HEURIST.edit.record.reminders.length; ++i) {
				new top.HEURIST.edit.Reminder(reminderDiv, parent.HEURIST.edit.record.reminders[i]);
			}
			window.reminderInput = new top.HEURIST.edit.inputs.ReminderInput(reminderDiv);

			// woot stuff
			HAPI.WOOT.DEFAULT_WOOT_PERMISSIONS  = [ HAPI.WOOT.OWNER_PROTECTION ];
			HAPI.WOOT.DEFAULT_CHUNK_PERMISSIONS = [ HAPI.WOOT.OWNER_PROTECTION ];

			var wootTitle = "bookmark:" + parent.HEURIST.edit.record.bkmkID;
			var recTitle = parent.HEURIST.edit.record.title + " - private notes";

			document.getElementById("woot-ext-link").href += "?w=" + wootTitle + "&t=" + recTitle + "&db=" + db;

//			HAPI.WOOT.GUI.labelsForLanguage.en.emptyMessage.label = "(empty chunk)";
			var wootEditor = new HAPI.WOOT.GUI.WootEditor({ title: wootTitle, element: document.getElementById("personalWoot") });


		</script>
	</body>
</html>
